const app = require('../express');
const student = require('../dao/studentDao');
const bodyParser = require('body-parser');

// 添加学生
app.post('/addStudent', bodyParser.json(), (req, res) => {
    let saccount = req.body.saccount;
    let sname = req.body.sname;
    let classid = req.body.classid;
    student.insertStudent(saccount, sname, classid, (result) => {
        res.writeHead(200);
        res.end(JSON.stringify({msg:'添加成功'}));
    }, (err)=>{
        res.end(JSON.stringify({msg:'添加学生失败'}));
    });
})

// 学生登录
app.post('/studentLogin',bodyParser.json(), (req, res) => {
    let saccount = req.body.account;
    let spwd = req.body.pwd;
    student.studentLogin(saccount, spwd, (result) => {
        if (result.length == 0) {
            res.end(JSON.stringify({
                msg: '账号或密码错误'
            }));
        }else{
            res.writeHead(200);
            res.end(JSON.stringify(result));
        }
    }, (err) => {
        res.end(JSON.stringify({
            msg: '账号或密码错误'
        }));
    })
})

// 查询学生的所有信息
app.get('/searchStudent', (req, res) => {
    student.searchStudent((result) => {
        res.writeHead(200);
        res.end(JSON.stringify(result));
    }, (err) => {
        console.log(err);
        res.end(JSON.stringify({msg:'查询失败'}));
    })
})

// 修改学生的姓名和班级
app.post('/updateStudent', bodyParser.json(), (req, res) => {
    let saccount = req.body.saccount;
    let sname = req.body.name;
    let classid = req.body.classid;
    student.updateStudent(sname, classid, saccount, (result) => {
        res.writeHead(200);
        res.end(JSON.stringify({msg:'修改成功'}));
    }, (err) => {
        res.end(JSON.stringify({msg:'修改失败'}));
    });
})

// 通过学号删除学生
app.post('/deleteStudent', bodyParser.json(), (req, res) => {
    let saccount = req.body.saccount;
    student.deleteStudent(saccount, (result) => {
        res.writeHead(200);
        res.end(JSON.stringify({msg:'删除成功'}));
    }, (err) => {
        res.end(JSON.stringify({msg:'删除失败'}));
    })
})

// 通过学号找学生id
app.post('/searchIdByAccount', bodyParser.json(), (req, res) => {
    let saccount = req.body.saccount;
    student.searchIdByAccount(saccount, (result) => {
        res.writeHead(200);
        res.end(JSON.stringify(result));
    }, (err) => {
        res.end(JSON.stringify({msg:'学号有误'}));
    })
});

// 根据id添加成绩
app.post('/insertScore', bodyParser.json(), (req, res) => {
    let sid = req.body.sid;
    let chinese = req.body.chineseS;
    let math = req.body.mathS;
    let english = req.body.englishS;
    student.insertScore(sid, chinese, math, english, (result) => {
        res.writeHead(200);
        res.end(JSON.stringify({msg:'修改成功'}));
    }, (err) => {
        res.end(JSON.stringify({msg:'修改失败'}));
    })
})

// 根据id删除成绩记录
app.post('/deleteScore', bodyParser.json(), (req, res) => {
    let sid = req.body.sid;
    student.deleteScore(sid, (result) => {
        res.writeHead(200);
        res.end(JSON.stringify({msg:'删除成绩成功'}));
    }, (err) => {
        res.end(JSON.stringify({msg:'删除成绩失败'}));
    })
})

// 查询学生数量
app.get('/countStudent', (req, res) => {
    student.countStudent((result) => {
        res.writeHead(200);
        res.end(JSON.stringify(result));
    }, (err) => {
        res.end(JSON.stringify({msg:'查询失败'}));
    })
})

// 查询某科的及格人数
app.post('/getScoreNum', bodyParser.json(), (req, res) => {
    let cid = req.body.cid;
    student.getScoreNum(cid, (result) => {
        res.writeHead(200);
        res.end(JSON.stringify(result));
    }, (err) => {
        res.end(err);
    })
})
